package com.zmn.oms.services.impl.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.constant.MqTagConsts;
import com.zmn.manager.mq.constant.MqTopicConsts;
import com.zmn.manager.mq.interfaces.ZmnMQSender;
import com.zmn.oms.constant.MessageQueueTopicConstant;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.order.OrderOperationFailMessage;
import com.zmn.oms.model.dto.order.OrderOperationInstructionMessage;
import com.zmn.oms.model.dto.order.create.CreateOrderFailMessage;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.services.interfaces.order.OrderChangeService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 类描述：订单改变实现
 *
 * @author lujia
 * @date 2018/11/30 21:10
 */
@Slf4j
@Service
public class OrderChangeServiceImpl implements OrderChangeService {

    private static final Logger logger = LoggerFactory.getLogger(OrderChangeServiceImpl.class);
    @Resource
    private ZmnMQSender zmnMQSender;

    // @Override
    // public void change(Long orderId) {
    //     String json = JSON.toJSONString(Lists.newArrayList(orderId));
    //     String key = StringUtil.getRandomStringAndNumber3(32);
    //     logger.debug("[order-change]---> orderId:{} key:{}", orderId, key);
    //     zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, MqTagConsts.ZMN_TOPIC_OMS_CHANGE_ORDER_WORK, key, json);
    // }

    @Override
    public void changeSyncToMos(Long orderId) {
        List<Long> orderIdList = Arrays.asList(orderId);
        String message = JSON.toJSONString(orderIdList);
        String key = String.format("%s-%s", orderId, "mos");
        log.info("同步到 mos：mq:{},tag:{},key:{},message:{}", MessageQueueTopicConstant.ZMN_TOPIC_MOS,
                OmsMqTagConsts.MOS_TAG_ORDER_WORK, key, message);
        // 延迟三秒执行 防止未提交事物 or 同步到es
//        long time = System.currentTimeMillis() + 3000;
//        zmnMQSender.sendWithStartDeliverTime(MessageQueueTopicConstant.ZMN_TOPIC_MOS, OmsMqTagConsts.MOS_TAG_ORDER_WORK, key, message, time);
        zmnMQSender.send(MessageQueueTopicConstant.ZMN_TOPIC_MOS, OmsMqTagConsts.MOS_TAG_ORDER_WORK, key, message);
    }

    @Override
    public void changeSyncDelayToMos(Long orderId, long delayTime) {
        List<Long> orderIdList = Arrays.asList(orderId);
        String message = JSON.toJSONString(orderIdList);
        String key = String.format("%s-%s", orderId, "mos");
        log.info("同步到 mos：mq:{},tag:{},key:{},message:{}", MessageQueueTopicConstant.ZMN_TOPIC_MOS,
                OmsMqTagConsts.MOS_TAG_ORDER_WORK, key, message);
        // 延迟执行 防止未提交事物 or 同步到es
        zmnMQSender.sendWithStartDeliverTime(MessageQueueTopicConstant.ZMN_TOPIC_MOS, OmsMqTagConsts.MOS_TAG_ORDER_WORK, key, message, delayTime);
    }

    // @Override
    // public void change(List<Long> orderIdList) {
    //
    //     if (CollectionUtils.isEmpty(orderIdList)) {
    //         return;
    //     }
    //
    //     String json = JSON.toJSONString(orderIdList);
    //     String key = StringUtil.getRandomStringAndNumber3(32);
    //
    //     logger.debug("[order-change] orderid:{} key:{}", json, key);
    //     zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, MqTagConsts.ZMN_TOPIC_OMS_CHANGE_ORDER_WORK, key, json);
    // }

    @Override
    public void changeOfInsOrder(OrderWorkLogChange orderWorkLogChange) {
        String key = "ins_" + StringUtil.getRandomStringAndNumber3(28);
        orderWorkLogChange.setMqMsgKey(key);
        String json = JSON.toJSONString(orderWorkLogChange);
        logger.info("[order-change-ins] info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, MqTagConsts.ZMN_TOPIC_OMS_CHANGE_ORDER_WORK_INS, key, json);
    }

    @Override
    public void changeStatusSync(OrderWorkLogChange orderWorkLogChange) {
        String key = "sync_status_" + StringUtil.getRandomStringAndNumber3(20);
        orderWorkLogChange.setNewOrder(false);
        orderWorkLogChange.setMqMsgKey(key);
        String json = JSON.toJSONString(orderWorkLogChange);
        logger.info("[order-sync-change] info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, MqTagConsts.ZMN_TOPIC_OMS_ORDER_WORK_SYNC_CHANGE, key, json);
    }

    @Override
    public void changeStatusSyncToApi(OrderWorkLogChange orderWorkLogChange) {
        String key = "sync_api_" + StringUtil.getRandomStringAndNumber3(20);
        orderWorkLogChange.setNewOrder(false);
        orderWorkLogChange.setMqMsgKey(key);
        String json = JSON.toJSONString(orderWorkLogChange);
        logger.info("[order-sync-api-change] info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, OmsMqTagConsts.ZMN_TOPIC_OMS_ORDER_WORK_SYNC_CHANGE_API, key, json);
    }

    @Override
    public void newOrderSync(OrderWorkLogChange orderWorkLogChange) {
        String key = "sync_new_" + StringUtil.getRandomStringAndNumber3(20);
        orderWorkLogChange.setNewOrder(true);
        orderWorkLogChange.setMqMsgKey(key);
        String json = JSON.toJSONString(orderWorkLogChange);
        logger.info("[order-sync-new] info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, MqTagConsts.ZMN_TOPIC_OMS_ORDER_WORK_SYNC_NEW, key, json);
    }

    @Override
    public void newOrderSyncToApi(OrderWorkLogChange orderWorkLogChange) {
        String key = "sync_anew_" + StringUtil.getRandomStringAndNumber3(19);
        orderWorkLogChange.setNewOrder(true);
        orderWorkLogChange.setMqMsgKey(key);
        String json = JSON.toJSONString(orderWorkLogChange);
        logger.info("[order-sync-api-new] info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, OmsMqTagConsts.ZMN_TOPIC_OMS_ORDER_WORK_SYNC_NEW_API, key, json);
    }

    @Override
    public void newEntrustOrderSyncToApi(OrderWorkLogChange orderWorkLogChange) {
        String key = "ent_sync_new_" + StringUtil.getRandomStringAndNumber3(19);
        orderWorkLogChange.setNewOrder(true);
        orderWorkLogChange.setMqMsgKey(key);
        String json = JSON.toJSONString(orderWorkLogChange);
        logger.info("[entrust-order-sync-api-new] info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, OmsMqTagConsts.ZMN_TOPIC_OMS_ENTRUST_ORDER_SYNC_NEW_API, key, json);
    }

    @Override
    public void changeStatusSyncTo58(OrderWorkLogChange orderWorkLogChange) {
        String key = "sync_stat58_" + StringUtil.getRandomStringAndNumber3(20);
        orderWorkLogChange.setNewOrder(false);
        orderWorkLogChange.setMqMsgKey(key);
        String json = JSON.toJSONString(orderWorkLogChange);
        logger.info("[order-sync-change58] info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, MqTagConsts.ZMN_TOPIC_OMS_ORDER_WORK_SYNC_CHANGE_TO_58, key, json);
    }

    @Override
    public void changeStatusSyncToUploadIASC(Long orderId, Integer logType) {
        String key = "sync_stat_iasc_" + StringUtil.getRandomStringAndNumber3(20);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderId", String.valueOf(orderId));
        jsonObject.put("logType", String.valueOf(logType));

        String json = jsonObject.toJSONString();
        logger.debug("[order-sync-change-upload-iasc] info:{} key:{}", json, key);

        // 修改同步
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, OmsMqTagConsts.ZMN_TOPIC_OMS_ORDER_WORK_SYNC_CHANGE_UPLOAD_IASC, key, json);
    }

    @Override
    public void orderCreateFailSyncToApi(CreateOrderFailMessage createOrderFailMessage) {
        String key = "sync_create_fail_" + StringUtil.getRandomStringAndNumber3(15);
        createOrderFailMessage.setMqMsgKey(key);
        // 消息处理次数
        if (createOrderFailMessage.getProcessTimes() == null) {
            createOrderFailMessage.setProcessTimes(0);
        }
        String json = JSON.toJSONString(createOrderFailMessage);
        logger.info("#order#sync#api#create#fail info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, OmsMqTagConsts.ZMN_TOPIC_OMS_ORDER_CREATE_FAIL_SYNC_API, key, json);
    }

    @Override
    public void orderOperationFailSyncToApi(OrderOperationFailMessage orderOperationFailMessage) {
        String key = "order_opt_fail_" + StringUtil.getRandomStringAndNumber3(17);
        orderOperationFailMessage.setMqMsgKey(key);
        String json = JSON.toJSONString(orderOperationFailMessage);
        logger.info("#order#operationFail info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, OmsMqTagConsts.ZMN_TOPIC_OMS_ORDER_OPERATION_FAIL_SYNC_API, key, json);
    }

    @Override
    public void orderOperationInstructionToApi(OrderOperationInstructionMessage orderOperationInstructionMessage) {
        String key = "order_opt_inst_" + StringUtil.getRandomStringAndNumber3(17);
        orderOperationInstructionMessage.setMqMsgKey(key);
        String json = JSON.toJSONString(orderOperationInstructionMessage);
        logger.info("#order#operationInstruction info:{} key:{}", json, key);
        zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, OmsMqTagConsts.ZMN_TOPIC_OMS_ORDER_OPERATION_INSTRUCTION_API, key, json);
    }
}
